<div class="page page-current" id="delivery-broken">
    <!-- 标题栏 -->
    <include file="Public:header_back"/>

    <div class="content">
        <div class="card facebook-card">
            <div class="card-header no-border">
                <a href="#">
                    <div class="facebook-avatar">
                        <img src="{$data.book.b_logo}" width="35" height="35">
                    </div>
                    <div class="facebook-name">《{$data.book.sm}》</div>
                    <div class="facebook-date">{$data.book.tm}</div>
                </a>
            </div>
            <div class="card-content content-padded">
                <p>借阅数量：{$data['quantity']-$data['num_backed']-$data['num_broken']} 本</p>
                <p>图书原价：{$data.book.dj} 元/本</p>
                <p>借阅时间：{$data.order_book.create_time}</p>
                <hr>
                <div class="list-block">
                    <form>
                        <input type="hidden" name="order_has_book_id" value="{$data.order_has_book_id}">
                        <ul>
                            <li>
                                <div class="item-content">
                                    <div class="item-media"><i class="icon icon-form-name"></i></div>
                                    <div class="item-inner">
                                        <div class="item-title label">回收数量</div>
                                        <div class="item-input">
                                            <input type="number" onkeyup="getMoney()" placeholder="本次收回数量" name="num" value="{$data['quantity']-$data['num_backed']-$data['num_broken']}">
                                        </div>
                                    </div>
                                </div>
                            </li>
                            <li>
                                <div class="item-content">
                                    <div class="item-media"><i class="icon icon-form-toggle"></i></div>
                                    <div class="item-inner">
                                        <div class="item-title label">破损回收</div>
                                        <div class="item-input">
                                            <label class="label-switch">
                                                <input type="checkbox" name="is_broken">
                                                <div class="checkbox"></div>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </li>
                            <div id="broken" hidden style="padding-left: 1rem;">
                                <li>
                                    <div class="item-content">
                                        <div class="item-media"><i class="icon icon-form-email"></i></div>
                                        <div class="item-inner">
                                            <div class="item-title label">回收金额</div>
                                            <div class="item-input">
                                                <input type="tel" placeholder="" name="money">
                                            </div>
                                        </div>
                                    </div>
                                </li>
                                <li>
                                    <div class="item-content">
                                        <div class="item-media"><i class="icon icon-form-gender"></i></div>
                                        <div class="item-inner">
                                            <div class="item-title label">支付方式</div>
                                            <div class="item-input">
                                                <select>
                                                    <option>现金</option>
                                                    <!--<option>微信支付</option>-->
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </li>
                                <!-- Date -->

                                <!--<li>-->
                                    <!--<div class="item-content">-->
                                        <!--<div class="item-media"><i class="icon icon-form-calendar"></i></div>-->
                                        <!--<div class="item-inner">-->
                                            <!--<div class="item-title label">收回图书</div>-->
                                            <!--<div class="item-input">-->
                                                <!--<label class="label-switch">-->
                                                    <!--<input type="checkbox" name="is_back">-->
                                                    <!--<div class="checkbox"></div>-->
                                                <!--</label>-->
                                            <!--</div>-->
                                        <!--</div>-->
                                    <!--</div>-->
                                <!--</li>-->

                            </div>

                            <!--<li class="align-top">-->
                            <!--<div class="item-content">-->
                            <!--<div class="item-media"><i class="icon icon-form-comment"></i></div>-->
                            <!--<div class="item-inner">-->
                            <!--<div class="item-title label">备注</div>-->
                            <!--<div class="item-input">-->
                            <!--<textarea></textarea>-->
                            <!--</div>-->
                            <!--</div>-->
                            <!--</div>-->
                            <!--</li>-->
                        </ul>
                    </form>
                </div>
            </div>
            <div class="card-footer ">
                <a href="#" class="button button-fill button-success" data-no-cache="true" style="color:#fff;">确定</a>
                <a href="#" class="button button-fill button-danger back" data-no-cache="true" style="color:#fff;">取消</a>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).on("pageInit", "#delivery-broken", function (e, pageId, $page) {
        $('#broken').hide();
    });

    //回收金额默认值
    function getMoney(){
        max = parseInt("{$data['quantity']-$data['num_backed']-$data['num_broken']}");
        $('[name="num"]').val($('[name="num"]').val()*1);
        if($('[name="num"]').val() <= 1){
            $('[name="num"]').val(1);
        }
        if($('[name="num"]').val() > max){
            $('[name="num"]').val(max);
        }
        var dj = parseInt("{$data.book.dj}");
        var sl = parseInt($('[name="num"]').val());
        $('[name="money"]').val((dj * sl * 0.7).toFixed(2));
    }
    getMoney();

    $(function () {
        $('[name="is_broken"]').on('change', function () {
            if ($(this).is(':checked')) {
                $('#broken').show();
                $('[name="num"]').val(1).attr("readonly","readonly");
            } else {
                $('#broken').hide();
                $('[name="num"]').removeAttr("readonly");
            }
            getMoney();
        });

        //回收金额默认值
//        function getMoney(){
//            var dj = parseInt("{$data.book.dj}");
//            var sl = parseInt("{$data['quantity']-$data['num_backed']-$data['num_broken']}");
//            $('[name="money"]').val(($('[name="money"]').val() * 0.7).toFixed(2));
//        }
//        $('[name="money"]').val(function () {
//            var dj = parseInt("{$data.book.dj}");
//            var sl = parseInt("{$data['quantity']-$data['num_backed']-$data['num_broken']}");
//            return (dj * sl * 0.7).toFixed(2);
//        });

        $('#delivery-broken .button-success').on('click', function () {
            if ($('[name="num"]').val() > parseInt("{$data['quantity']-$data['num_backed']-$data['num_broken']}")) {
                $.alert('归还数量超过借阅数量');
                return;
            }

            if ($('[name="is_broken"]').is(':checked')) {   //是否破损 破损
                $.confirm('收取金额:' + $('[name="money"]').val() + ($('[name="is_back"]').is(':checked') ? '  同时收回图书' : '  图书留给客户'), '破损回收', function () {
//                    console.log($('form').serialize());
//                    return;
                    $.post("{:U('brokenBack')}",
                            $('form').serialize(),
                            function(data,status){
                                if (status == 'success') {
                                    data = $.parseJSON(data);
                                    if (data.code != 1) {
                                        $.alert(data.msg);
                                    } else {
                                        $.alert(data.msg, function () {
                                            $.router.load("{:U('Delivery/borrow',['user_id'=>$data['order_book']['user_id']])}", true);
                                        });
                                    }
                                } else {
                                    $.toast('网络错误，请重试');
                                }
                            }
                    );
                });
            } else {                                        //未破损
                $.confirm('确认图书完好？', '直接回收', function () {
                    $.post("{:U('doBack')}",
                        $('form').serialize(),
                        function (data, status) {
                            if (status == 'success') {
                                data = $.parseJSON(data);
                                if (data.code != 1) {
                                    $.alert(data.msg);
                                } else {
                                    $.alert(data.msg, function () {
                                        $.router.load("{:U('Delivery/borrow',['user_id'=>$data['order_book']['user_id']])}", true);
                                    });
                                }
                            } else {
                                $.toast('网络错误，请重试');
                            }
                        }
                    );
                });
            }
        });
    });
</script>